package net.lanelife.casino.caipiao.job;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import net.lanelife.casino.caipiao.entity.Job;
import net.lanelife.casino.caipiao.service.api.IJobService;

import org.springframework.beans.factory.annotation.Autowired;

public class JobExecutor implements Runnable {
	
	private static Executor executor;
	
	@Autowired
	private IJobService jobService;
	
	private int limit;
	
	private int poolSize;
	
	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPoolSize() {
		return poolSize;
	}

	public void setPoolSize(int poolSize) {
		this.poolSize = poolSize;
	}
	
	public void init() {
		executor = Executors.newFixedThreadPool(poolSize);
	}

	@Override
	public void run() {
		List<Job> jobs = jobService.selectJobs(limit);
		if (jobs!=null) {
			for (final Job job : jobs) {
				executor.execute(new Runnable() {
					@Override
					public void run() {
						jobService.executeJob(job);
					}
				});
			}
		}
	}

}
